package com.example.xupdateLearningProject.http;

import androidx.annotation.NonNull;

import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.XHttpSDK;
import com.xuexiang.xhttp2.callback.CallBack;
import com.xuexiang.xhttp2.callback.DownloadProgressCallBack;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xupdate.proxy.IUpdateHttpService;
import com.xuexiang.xupdate.utils.FileUtils;
import com.xuexiang.xutil.tip.ToastUtils;

import java.util.Map;

import io.reactivex.disposables.Disposable;

public class XHttpUpdateHttpService implements IUpdateHttpService {
    private String baseUrl;
    public XHttpUpdateHttpService(String baseUrl){
        this.baseUrl = baseUrl;
    }
    @Override
    public void asyncGet(@NonNull String url, @NonNull Map<String, Object> params, @NonNull Callback callBack) {
        XHttp.get(url)
                .baseUrl(baseUrl)
                .params(params)
                .keepJson(true)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String response) throws Throwable {
                        callBack.onSuccess(response);
                    }

                    @Override
                    public void onError(ApiException e) {
                        callBack.onError(e);
                    }
                });
    }

    @Override
    public void asyncPost(@NonNull String url, @NonNull Map<String, Object> params, @NonNull Callback callBack) {
        XHttp.post(url)
                .baseUrl(baseUrl)
                .params(params)
                .keepJson(true)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String response) throws Throwable {
                        callBack.onSuccess(response);
                    }

                    @Override
                    public void onError(ApiException e) {
                        callBack.onError(e);
                    }
                });
    }

    @Override
    public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, @NonNull DownloadCallback callback) {
        Disposable disposable = XHttp.downLoad(url)
                .savePath(path)
                .saveName(fileName)
                .isUseBaseUrl(false)
                .baseUrl(baseUrl)
                .execute(new DownloadProgressCallBack<String>() {
                    @Override
                    public void update(long downLoadSize, long totalSize, boolean done) {
                        callback.onProgress(downLoadSize / (float)totalSize,totalSize);
                    }

                    @Override
                    public void onComplete(String path) {
                        callback.onSuccess(FileUtils.getFileByPath(path));
                    }

                    @Override
                    public void onStart() {
                        callback.onStart();
                    }

                    @Override
                    public void onError(ApiException e) {
                        callback.onError(e);
                    }
                });
        XHttpSDK.addRequest(url,disposable);
    }

    @Override
    public void cancelDownload(@NonNull String url) {
        ToastUtils.toast("已取消更新");
        XHttpSDK.cancelRequest(url);
    }
}
